﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using DYSales.Views.sell;

namespace DYSales.Views.total
{
    public partial class ExpsTotal_page : Page
    {
        public ExpsTotal_page()
        {
            InitializeComponent();
            dg_exps.AutoGeneratingColumn += other.GetOther().dg_AutoGeneratingColumn;
            Loaded += new RoutedEventHandler(ExpsTotal_page_Loaded);
            bt_查询.Click += new RoutedEventHandler(bt_查询_Click);
            bt_删除.Click += new RoutedEventHandler(bt_删除_Click);
            bt_增加支出.Click += new RoutedEventHandler(bt_增加支出_Click);
            bt_显示所有.Click += new RoutedEventHandler(bt_显示所有_Click);
        }

        void bt_显示所有_Click(object sender, RoutedEventArgs e)
        {
            dm.getDS().Load(dm.getDS().GetExpenditureTablesQuery(), lo =>
            {
                if (!lo.HasError)
                {
                    dg_exps.DataContext = lo.Entities;
                }
            }, null);
        }

        void bt_增加支出_Click(object sender, RoutedEventArgs e)
        {
            dm.getDS().GetST(lo =>
            {
                var expWin = new cwin_expAdd();
                expWin.Closed += new EventHandler(expWin_Closed);
                var et = new Web.ExpenditureTable();
                et.exp_备注 = string.Empty;
                et.exp_时间 = lo.Value;
                et.exp_支出金额 = 0;
                expWin.CurrentItem = et;
                expWin.Show();
            }, null);
        }

        void expWin_Closed(object sender, EventArgs e)
        {
            var cwin = sender as cwin_expAdd;
            cwin.Closed -= new EventHandler(expWin_Closed);
            if (cwin.DialogResult.Value)
            {
                dm.getDS().ExpenditureTables.Add(cwin.CurrentItem);
                dm.getDS().SubmitChanges(lo => { refresh(); }, null);
            }
        }

        void bt_删除_Click(object sender, RoutedEventArgs e)
        {
            if (dg_exps.Selection.SelectedRows.Count >= 1)
            {
                var daqWin = new cwin_Question();
                daqWin.Closed += new EventHandler(daqWin_Closed);
                daqWin.Title = "东雅进销存提示";
                daqWin.tbk_text.Text = "确定要删除选中的记录吗?";
                daqWin.Show();
            }
        }

        void daqWin_Closed(object sender, EventArgs e)
        {
            var win = sender as cwin_Question;
            win.Closed -= new EventHandler(daqWin_Closed);
            if (win.DialogResult.Value)
            {
                foreach (var item in dg_exps.Selection.SelectedRows)
                {
                    dm.getDS().ExpenditureTables.Remove(item.DataItem as Web.ExpenditureTable);
                }
                dm.getDS().SubmitChanges(lo =>
                {
                    refresh();
                }, null);
            }
        }

        void bt_查询_Click(object sender, RoutedEventArgs e)
        {
            refresh();
        }

        void ExpsTotal_page_Loaded(object sender, RoutedEventArgs e)
        {
            dm.getDS().GetST(lt =>
            {
                dm.getDS().GetSetTable(ls =>
                {
                    var lttime = lt.Value;
                    var lstime = ls.Value.s_营业开始时间;
                    var lstime2 = ls.Value.s_营业结束时间;
                    tb_startTime.DateTime = new DateTime(lttime.Year, lttime.Month, lttime.Day, lstime.Hour, lstime.Minute, lstime.Second);
                    tb_lasttime.DateTime = new DateTime(lttime.Year, lttime.Month, lttime.Day, lstime2.Hour, lstime2.Minute, lstime2.Second).AddDays(1);
                    refresh();
                }, null);
            }, null);
        }

        void refresh()
        {
            dm.getDS().Load(dm.getDS().GetExpenditureTableByTimeQuery(tb_startTime.DateTime.Value, tb_lasttime.DateTime.Value), lo =>
            {
                if (!lo.HasError)
                {
                    dg_exps.DataContext = lo.Entities;
                }
            }, null);
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

    }
}
